package com.loongtech.bi.action.monitor;

import com.loongtech.bi.action.authManagement.ResultData;
import com.loongtech.bi.entity.POJO.Page;
import com.loongtech.bi.entity.system.EntityMonitorConfig;
import com.loongtech.bi.manager.system.BiMonitorConfigManager;
import com.loongtech.bi.manager.system.BiMonitorGroupConfigManager;
import com.loongtech.bi.manager.system.SyncInfoLogManager;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.support.Session;
import com.loongtech.core.util.RetCode;
import com.loongtech.core.util.Utils;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * Author: yuri
 * Description: 监控语句配置
 * Date: 16:43 2017/5/17
 */
@RestController
@Scope("prototype")
@RequestMapping("/monitorconfig")
public class MonitorConfigAction {
    @Resource
    private BiMonitorConfigManager biMonitorConfigManager;
    @Resource
    private BiMonitorGroupConfigManager groupManager;
    @Resource
    private Session session;
    @Resource
    private SysLogManager sysLogManager;
    @Resource
    private SyncInfoLogManager syncInfoLogManager;

    /**
     * 查询任务
     */
    @RequestMapping(value = "list.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<?> searchByCriteria(@RequestParam Integer curPage, @RequestParam Integer pageSize, EntityMonitorConfig entity) {
        Page<List<EntityMonitorConfig>> res;
        try {
            res = biMonitorConfigManager.queryByCriteria(curPage, pageSize, entity);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
        return new ResultData<>(RetCode.eSuccess.getErrorcode(), res);
    }

    /**
     * 添加并开始任务
     */
    @RequestMapping(value = "addStart.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<String> addEntityMonitorConfig(@RequestParam Long starttime, @RequestParam Long endtime, EntityMonitorConfig entity) {
        if (starttime == null || endtime == null || (starttime >= endtime)) {
            return new ResultData<>(RetCode.eIlegalTime.getErrorcode(), RetCode.eIlegalTime.getErrorMsg());
        }
        if (validate(entity) != null) {
            return validate(entity);
        }
        try {
            EntityMonitorConfig res = biMonitorConfigManager.getByQuery("from EntityMonitorConfig where id = ?", entity.getId());
            if (res != null) {
                return new ResultData<>(RetCode.eIdExist.getErrorcode(), RetCode.eIdExist.getErrorMsg());
            }
            biMonitorConfigManager.addEntityMonitorConfig(starttime, endtime, entity);
            sysLogManager.addLog(session.getUserName(), "增加监控任务成功:" + entity.getDescription());
            return ResultData.success();
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
    }

    /**
     * 修改
     */
    @RequestMapping(value = "modify.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<String> modifyEntityMonitorConfig(@RequestParam Long starttime, @RequestParam Long endtime, EntityMonitorConfig entity) {
        if (validate(entity) != null) {
            return validate(entity);
        }
        if (starttime == null || endtime == null || (starttime >= endtime)) {
            return new ResultData<>(RetCode.eIlegalTime.getErrorcode(), RetCode.eIlegalTime.getErrorMsg());
        }
        try {
            EntityMonitorConfig cur = biMonitorConfigManager.get(entity.getId());
            if (cur != null) {
                entity.setCreatTime(cur.getCreatTime());
                biMonitorConfigManager.modifyEntityMonitorConfig(starttime, endtime, entity);
                sysLogManager.addLog(session.getUserName(), "监控任务修改成功" + entity.getDescription());
                return ResultData.success();
            } else {
                return new ResultData<>(RetCode.eNotExistGourp.getErrorcode(), RetCode.eNotExistGourp.getErrorMsg());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
    }

    /**
     * 全部开始
     */
    @RequestMapping(value = "startAll.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<String> startAllEntityMonitorConfig(@RequestParam(required = false) String gameId) {
        try {
            biMonitorConfigManager.startAllEntityMonitorConfig(gameId);
            sysLogManager.addLog(session.getUserName(), "监控任务全部开始");
            return ResultData.success();
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
    }

    /**
     * 废除任务
     */
    @RequestMapping(value = "abandonJob.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<String> delEntityMonitorConfig(@RequestParam(value = "id") Integer id) {
        try {
            biMonitorConfigManager.delEntityMonitorConfig(id.toString());
            sysLogManager.addLog(session.getUserName(), "监控任务停止" + id);
            return ResultData.success();
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
    }

    /**
     * 暂停
     */
    @RequestMapping(value = "pauseJob.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<String> pauseScheduleJob(@RequestParam(value = "id") String scheduleJobId) {
        try {
            biMonitorConfigManager.pauseMonitor(scheduleJobId);
            sysLogManager.addLog(session.getUserName(), "监控任务暂停" + scheduleJobId);
            return ResultData.success();
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
    }

    /**
     * 恢复
     */
    @RequestMapping(value = "resumeJob.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<String> resumeScheduleJob(@RequestParam Long starttime, @RequestParam Long endtime, EntityMonitorConfig e) {
        if (validate(e) != null) {
            return validate(e);
        }
        try {
            biMonitorConfigManager.resumeMonitor(starttime, endtime, e);
            sysLogManager.addLog(session.getUserName(), "监控任务恢复" + e.getDescription());
            return ResultData.success();
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
    }

    /**
     * 全部暂停
     */
    @RequestMapping(value = "pauseAll.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<String> pauseAllEntityMonitorConfig(@RequestParam(required = false) String gameId) {
        try {
            biMonitorConfigManager.pauseAllEntityMonitorConfig(gameId);
            sysLogManager.addLog(session.getUserName(), "监控任务全部暂停成功");
            return ResultData.success();
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
    }

    /**
     * 全部恢复
     */
    @RequestMapping(value = "resumeAll.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<String> resumeAllEntityMonitorConfig(@RequestParam(required = false) String gameId) {
        try {
            biMonitorConfigManager.resumeAllEntityMonitorConfig(gameId);
            sysLogManager.addLog(session.getUserName(), "监控任务全部恢复成功");
            return ResultData.success();
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
    }


    private ResultData<String> validate(EntityMonitorConfig emc) {
        if (emc.getGameId() == null) {
            return new ResultData<>(RetCode.eEmptyId.getErrorcode(), RetCode.eEmptyId.getErrorMsg());
        }
        if (null != emc.getCountNum()) {
            if (emc.getCountNum() <= 0) {
                return new ResultData<>(RetCode.eMinusValueMonitorTime.getErrorcode(), RetCode.eMinusValueMonitorTime.getErrorMsg());
            } else if (emc.getCountNum() > Integer.MAX_VALUE) {
                emc.setCountNum(Integer.MAX_VALUE);
            }
        }
        if (null == emc.getGroupId() || emc.getGroupId() < 0) {
            return new ResultData<>(RetCode.eNotExistGroupId.getErrorcode(), RetCode.eNotExistGroupId.getErrorMsg());
        }

        if (null == emc.getThreshHold()) {
            return new ResultData<>(RetCode.eEmptyThresh.getErrorcode(), RetCode.eEmptyThresh.getErrorMsg());
        }

        if (null == emc.getIntervalTime() || emc.getIntervalTime() <= 0) {
            return new ResultData<>(RetCode.eIlegalInterval.getErrorcode(), RetCode.eIlegalInterval.getErrorMsg());
        }
        if (Utils.isEmptyStr(emc.getInquerySQL())) {
            return new ResultData<>(RetCode.eEmptyQuery.getErrorcode(), RetCode.eEmptyQuery.getErrorMsg());
        }
        return null;
    }
}
